<?php

namespace app\api\controller\port;

use app\common\controller\Api;

use think\Exception;

/**
 * 首页接口
 * 
 */
class Homepage extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 轮播图
     * 
     */
    public function getBanner()
    {
        $banner = db('banner')
                ->where(['switch'=>1])
                ->field('id,image')
                ->order('weigh desc,id desc')
                ->select();
        foreach($banner as &$val){
            $val['image'] = cdnurl($val['image'],true);
        }

        $this->success('Success', $banner);
    }


    /**
     * 首页推荐路线
     *
     */
    public function getTLine()
    {
        $limit = I('post.limit',10);
        $list = db('line')
            ->where('switch',1)
            ->field('id line_id,line_name,sharing_price,start_station,end_station')
            ->order('weigh desc')
            ->limit($limit)
            ->select();

        $this->success('Success', $list);
    }


    /**
     * 查询路线班次
     * 
     */
    public function getLintBanc()
    {
        $line_id = I('post.line_id',0);
        $line_id || $this->error('参数有误');
        $search_time = I('post.search_time');
        if(empty($search_time)){
            $search_time = date('Y-m-d');
        }

        $line_count = db('line')->where('id',$line_id)->count();
        $line_count || $this->error('没有找到路线信息');

        $search_time_begin = strtotime($search_time);
        $search_time_end = $search_time_begin + 86400;
        $where = [
            'b.line_id' => $line_id,
            'b.depart_time' =>[['>=',$search_time_begin],['<',$search_time_end]]
        ];
        $list = db('line_banc b')
            ->join('line l','l.id=b.line_id','LEFT')
            ->field('b.id banc_id,b.line_id,b.depart_time,b.distance,b.distance_time,l.line_name,l.sharing_price,l.all_price,l.start_station,l.start_tags,l.end_station,l.end_tags,l.way,l.is_jiesong,b.ticket_num')
            ->where($where)
            ->order('b.weigh desc')
            ->select();
        foreach($list as &$val){
            $val['depart_time_begin'] = date('H:i',$val['depart_time']);
            $val['depart_time_date'] = date('m-d',$val['depart_time']);
        }
        // dump(db('line_banc')->getLastSql());
        $this->success('Success', $list);
    }


    /**
     * 获取班次详情(可缓存)
     * 出发时间未做............
     * 
     */
    public function getLintDetail()
    {
        $banc_id = I('post.banc_id',0);
        $banc_id || $this->error('参数有误');

        $banc_data = db('line_banc')->where('id',$banc_id)->field('weigh,create_time',true)->find();
        $banc_data || $this->error('没有找到班次信息');
        $banc_data['depart_time_begin'] = date('H:i',$banc_data['depart_time']);
        $banc_data['depart_time_date'] = date('m-d',$banc_data['depart_time']);

        $line_station = db('line_station')
            ->where('line_id',$banc_data['line_id'])
            ->field('create_time',true)
            ->order('weigh desc')
            ->select();
        foreach($line_station as &$val){
            if($val['map_json']){
                $val['map_json'] = json_decode($val['map_json'],true);
            }
            $val['station_time'] = '10:20';
        }
        $banc_data['line_station'] = $line_station;

        $banc_data['line'] = db('line')->where('id',$banc_data['line_id'])->field('id,line_name,sharing_price,all_price')->find();

        $driver_data = db('driver')->where('id',$banc_data['driver_id'])->field('user_id,create_time',true)->find();
        if($driver_data){
            $driver_data['avatar_image'] = cdnurl($driver_data['avatar_image'],true);
        }
        $banc_data['driver_data'] = $driver_data;

        $this->success('Success', $banc_data);

    }


    /**
     * 搜索起始站
     * 需要做缓存
     * 
     */
    public function beginStation()
    {
        $area = db('line_station')->where('is_shang',1)->field('line_id,area')->group('area')->select();

        $station = $area;
        foreach($station as &$val){
            $shang = db('line_station')->where(['area'=>$val['area'],'is_shang'=>1,'line_id'=>$val['line_id']])
                ->field('line_id,station_id,station_name,is_shang,is_xia,coordinate_lat,coordinate_lng')->order('weigh desc')->select();
            $xia = db('line_station')->where(['is_xia'=>1,'line_id'=>$val['line_id']])
                ->field('line_id,station_id,station_name,is_shang,is_xia')->order('weigh desc')->select();

            $cloum = array_column($xia,'station_name');
            $end_station = join('、',$cloum);
            foreach($shang as &$s){
                $s['end_station'] = $end_station;
            }
            $val['child'] = $shang;
        }
        

        $this->success('Success', compact('area','station'));
    }

 
    /**
     * 搜索终点站
     * 
     */
    public function endStation()
    {
        $station_id = I('post.station_id');
        empty($station_id) && $this->error('参数有误');
        $line_ids = db('line_station')->where(['station_id'=>$station_id,'is_shang'=>1])->column('line_id');

        $area = db('line_station')->where('is_xia',1)->whereIn('line_id',$line_ids)->field('line_id,area')->group('area')->select();
        $station = $area;
        foreach($station as &$val){
            $xia = db('line_station')->where(['area'=>$val['area'],'is_xia'=>1,'line_id'=>$val['line_id']])
                ->field('line_id,station_id,station_name,is_shang,is_xia,coordinate_lat,coordinate_lng')->order('weigh desc')->select();

            $val['child'] = $xia;
        }

        $this->success('Success', compact('area','station'));
    }


    /**
     * 获取系统信息
     * 
     */
    public function sysConfig()
    {
        $kefu = config('site.kefu');
        $buy_notice = config('site.buy_notice');

        $this->success('Success', compact('kefu','buy_notice'));
    }



}
